#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QPushButton>
#include <QSerialPort>
#include <QPainter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 美化按钮和文本框
    ui->pushButton->setStyleSheet("QPushButton { background-color: rgba(0, 122, 204, 200); color: white; border-radius: 10px; font-size: 18px; padding: 8px 24px; } QPushButton:hover { background-color: #0078d7; }");
    ui->lineEdit->setStyleSheet("QLineEdit { background: rgba(255,255,255,180); border: 1px solid #aaa; border-radius: 8px; font-size: 16px; padding: 4px 10px; }");
        QPushButton *button=new QPushButton(this);
        button->move(100,200);
        button->setText("hello longer");
//        connect(button,SIGNAL(clicked()), ui->lineEdit, SLOT(close()));
//        disconnect(button, SIGNAL(clicked()), this, SLOT(close()));
        serial10 = new QSerialPort;
        serialOpen = false; // Initialize serialOpen state
        connect(serial10, &QSerialPort::readyRead, this, &Widget::serialread);
}

Widget::~Widget()
{
}


void Widget::on_pushButton_2_clicked()
{
    if (!serialOpen) {
        serial10->setPortName("COM10");
        serial10->setBaudRate(QSerialPort::Baud9600);
        serial10->setParity(QSerialPort::NoParity);
        serial10->setStopBits(QSerialPort::OneStop);
        if (serial10->open(QIODevice::ReadWrite)) {
            serial10->write("1");
            ui->pushButton->setText("关闭串口");
            serialOpen = true;
            qDebug() << "串口已打开";
        } else {
            qDebug() << "串口打开失败";
        }
    } else {
        serial10->close();
        ui->pushButton->setText("打开串口");
        serialOpen = false;
        qDebug() << "串口已关闭";
    }
}
void Widget::serialread() {
    QByteArray data = serial10->readAll();
    ui->lineEdit->setText(data);
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap bg(":/new/prefix1/background.png");
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.drawPixmap(rect(), bg.scaled(size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
    QWidget::paintEvent(event);
}

void Widget::on_pushButton_forward_pressed()
{
    serial10->write("F");
}

void Widget::on_pushButton_back_pressed()
{
    serial10->write("B");
}

void Widget::on_pushButton_left_pressed()
{
    serial10->write("L");
}



void Widget::on_pushButton_Stop_pressed()
{
    serial10->write("S");
}

void Widget::on_MOD_pressed()
{
    // 切换模式
    isRemoteMode = !isRemoteMode;
    if (isRemoteMode) {
        ui->MOD->setText("遥控");
    } else {
        ui->MOD->setText("循迹");
    }
    serial10->write("M");
}

void Widget::on_pushButton_forward_released()
{
     serial10->write("S");
}
void Widget::on_pushButton_back_released()
{
    serial10->write("S");
}

void Widget::on_pushButton_left_released()
{
    serial10->write("S");
}


void Widget::on_pushButton_Right_released()
{
    serial10->write("S");
}

void Widget::on_pushButton_Right_pressed()
{
    serial10->write("R");
}
